subpanel.c 6.24 KB
Newer Older
Monty Montgomery's avatar
 
Monty Montgomery committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 *
 *  postfish
 *    
 *      Copyright (C) 2002-2004 Monty
 *
 *  Postfish is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  Postfish is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with Postfish; see the file COPYING.  If not, write to the
 *  Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * 
 */

#include "postfish.h"
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "mainpanel.h"
#include "windowbutton.h"
#include "subpanel.h"

Monty Montgomery's avatar
 
Monty Montgomery committed
31
static int subpanel_hide(GtkWidget *widget,
Monty Montgomery's avatar
 
Monty Montgomery committed
32 33 34
			  GdkEvent *event,
			  gpointer in){
  subpanel_generic *p=in;
Monty Montgomery's avatar
 
Monty Montgomery committed
35
  if(p->mappedvar)*p->mappedvar=0;
Monty Montgomery's avatar
 
Monty Montgomery committed
36
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p->subpanel_windowbutton),0);
Monty Montgomery's avatar
 
Monty Montgomery committed
37
  return TRUE;
Monty Montgomery's avatar
 
Monty Montgomery committed
38
}
Monty Montgomery's avatar
 
Monty Montgomery committed
39

Monty Montgomery's avatar
 
Monty Montgomery committed
40 41 42 43 44 45 46 47 48 49 50 51
static int windowbutton_action(GtkWidget *widget,
			gpointer in){
  int active;
  subpanel_generic *p=in;
  if(widget==p->subpanel_windowbutton){
    active=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(p->subpanel_windowbutton));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p->mainpanel_windowbutton),active);
  }else{
    active=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(p->mainpanel_windowbutton));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p->subpanel_windowbutton),active);
  }
  
Monty Montgomery's avatar
 
Monty Montgomery committed
52 53
  if(active){
    if(p->mappedvar)*p->mappedvar=1;
Monty Montgomery's avatar
 
Monty Montgomery committed
54
    gtk_widget_show(p->subpanel_toplevel);
Monty Montgomery's avatar
 
Monty Montgomery committed
55 56
  }else{
    if(p->mappedvar)*p->mappedvar=0;
Monty Montgomery's avatar
 
Monty Montgomery committed
57
    gtk_widget_hide(p->subpanel_toplevel);
Monty Montgomery's avatar
 
Monty Montgomery committed
58
  }
Monty Montgomery's avatar
 
Monty Montgomery committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

  return FALSE;
}

static int activebutton_action(GtkWidget *widget,
			gpointer in){
  subpanel_generic *p=in;
  int active;

  if(widget==p->subpanel_activebutton){
    active=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(p->subpanel_activebutton));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p->mainpanel_activebutton),active);
  }else{
    active=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(p->mainpanel_activebutton));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p->subpanel_activebutton),active);
  }
  
  *p->activevar=active;
  return FALSE;
}

Monty Montgomery's avatar
 
Monty Montgomery committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static gboolean rebind_space(GtkWidget *widget,
			     GdkEventKey *event,
			     gpointer in){
  /* do not capture Alt accellerators */
  if(event->state&GDK_MOD1_MASK) return FALSE;
  if(event->state&GDK_CONTROL_MASK) return FALSE;

  if(event->keyval==GDK_space){
    subpanel_generic *p=in;
    GdkEvent copy=*(GdkEvent *)event;
    copy.any.window=p->mainpanel->toplevel->window;
    gtk_main_do_event((GdkEvent *)(&copy));
    return TRUE;
  }
  return FALSE;
}

Monty Montgomery's avatar
 
Monty Montgomery committed
97 98 99 100 101 102 103 104 105 106
static gboolean forward_events(GtkWidget *widget,
			       GdkEvent *event,
			       gpointer in){
  subpanel_generic *p=in;
  GdkEvent copy=*(GdkEvent *)event;
  copy.any.window=p->mainpanel->toplevel->window;
  gtk_main_do_event((GdkEvent *)(&copy));
  return TRUE;
}

Monty Montgomery's avatar
 
Monty Montgomery committed
107 108 109 110
void subpanel_show_all_but_toplevel(subpanel_generic *s){
  gtk_widget_show_all(s->subpanel_topframe);
}

Monty Montgomery's avatar
 
Monty Montgomery committed
111 112 113 114
subpanel_generic *subpanel_create(postfish_mainpanel *mp,
				  GtkWidget *windowbutton,
				  GtkWidget *activebutton,
				  sig_atomic_t *activevar,
Monty Montgomery's avatar
 
Monty Montgomery committed
115
				  sig_atomic_t *mappedvar,
Monty Montgomery's avatar
 
Monty Montgomery committed
116 117 118 119 120 121 122 123 124 125 126 127 128
				  char *prompt,char *shortcut){

  subpanel_generic *panel=calloc(1,sizeof(*panel));

  GtkWidget *toplabelbox=gtk_event_box_new();
  GtkWidget *toplabelframe=gtk_frame_new(NULL);
  GtkWidget *toplabel=gtk_hbox_new(0,0);
  GtkWidget *toplabelwb=windowbutton_new(prompt);
  GtkWidget *toplabelab=0;
  
  if(activebutton && activevar)
    toplabelab=gtk_toggle_button_new_with_label(shortcut);
  
Monty Montgomery's avatar
 
Monty Montgomery committed
129
  panel->subpanel_topframe=gtk_frame_new (NULL);
Monty Montgomery's avatar
 
Monty Montgomery committed
130 131 132 133 134 135
  panel->subpanel_windowbutton=toplabelwb;
  panel->subpanel_activebutton=toplabelab;

  panel->mainpanel_windowbutton=windowbutton;
  panel->mainpanel_activebutton=activebutton;
  panel->activevar=activevar;
Monty Montgomery's avatar
 
Monty Montgomery committed
136
  panel->mappedvar=mappedvar;
Monty Montgomery's avatar
 
Monty Montgomery committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  
  gtk_box_pack_start(GTK_BOX(toplabel),toplabelwb,0,0,5);
  gtk_box_pack_end(GTK_BOX(toplabel),toplabelab,0,0,5);

  gtk_widget_set_name(toplabelwb,"panelbutton");
  gtk_widget_set_name(toplabelbox,"panelbox");
  gtk_frame_set_shadow_type(GTK_FRAME(toplabelframe),GTK_SHADOW_ETCHED_IN);
  gtk_container_add(GTK_CONTAINER (toplabelbox), toplabelframe);
  gtk_container_add(GTK_CONTAINER (toplabelframe), toplabel);

  panel->subpanel_box=gtk_vbox_new (0,0);

  panel->subpanel_toplevel=gtk_window_new (GTK_WINDOW_TOPLEVEL);
  panel->mainpanel=mp;

Monty Montgomery's avatar
 
Monty Montgomery committed
152 153 154
  gtk_container_add (GTK_CONTAINER (panel->subpanel_toplevel), panel->subpanel_topframe);
  gtk_container_add (GTK_CONTAINER (panel->subpanel_topframe), panel->subpanel_box);
  gtk_container_set_border_width (GTK_CONTAINER (panel->subpanel_topframe), 3);
Monty Montgomery's avatar
 
Monty Montgomery committed
155
  gtk_container_set_border_width (GTK_CONTAINER (panel->subpanel_box), 5);
Monty Montgomery's avatar
 
Monty Montgomery committed
156 157
  gtk_frame_set_shadow_type(GTK_FRAME(panel->subpanel_topframe),GTK_SHADOW_NONE);
  gtk_frame_set_label_widget(GTK_FRAME(panel->subpanel_topframe),toplabelbox);
Monty Montgomery's avatar
 
Monty Montgomery committed
158 159

    
Monty Montgomery's avatar
 
Monty Montgomery committed
160 161 162 163
  /* space *always* means play/pause */
  g_signal_connect (G_OBJECT (panel->subpanel_toplevel), "key-press-event",
		    G_CALLBACK (rebind_space), 
		    panel);
Monty Montgomery's avatar
 
Monty Montgomery committed
164 165 166 167 168 169
  /* forward unhandled events to the main window */
  g_signal_connect_after (G_OBJECT (panel->subpanel_toplevel), "key-press-event",
			  G_CALLBACK (forward_events), 
			  panel);

  /* delete should == hide */
Monty Montgomery's avatar
 
Monty Montgomery committed
170
  g_signal_connect (G_OBJECT (panel->subpanel_toplevel), "delete-event",
Monty Montgomery's avatar
 
Monty Montgomery committed
171
		    G_CALLBACK (subpanel_hide), 
Monty Montgomery's avatar
 
Monty Montgomery committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		    panel);

  /* link the mainpanel and subpanel buttons */
  g_signal_connect_after (G_OBJECT (panel->mainpanel_windowbutton), "clicked",
			  G_CALLBACK (windowbutton_action), panel);
  g_signal_connect_after (G_OBJECT (panel->subpanel_windowbutton), "clicked",
			  G_CALLBACK (windowbutton_action), panel);
  if(activebutton && activevar){
    g_signal_connect_after (G_OBJECT (panel->mainpanel_activebutton), "clicked",
			    G_CALLBACK (activebutton_action), panel);
    g_signal_connect_after (G_OBJECT (panel->subpanel_activebutton), "clicked",
			    G_CALLBACK (activebutton_action), panel);
  }

  gtk_window_set_resizable(GTK_WINDOW(panel->subpanel_toplevel),0);

  return panel;
}