make sure retval is not used uninitialized
[AROS-Contrib.git] / vpdf / mcc / toolbar_class.c
blobacbab87032b1706cf20bed02ca6069be8fe5da4f
1 /*
2 */
4 #include <proto/muimaster.h>
5 #include <libraries/mui.h>
7 #include <proto/exec.h>
8 #include <proto/utility.h>
9 #include <exec/libraries.h>
10 #include <graphics/gfx.h>
11 #include <proto/alib.h>
12 #include <proto/graphics.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <math.h>
17 #include <limits.h>
18 #include <string.h>
19 #include <constructor.h>
20 #include <intuition/pointerclass.h>
22 #define SYSTEM_PRIVATE
23 #include <proto/cybergraphics.h>
24 #include <cybergraphx/cybergraphics.h>
25 #undef SYSTEM_PRIVATE
27 #if defined(__AROS__)
28 #include <clib/arossupport_protos.h>
29 #endif
31 #include <private/vapor/vapor.h>
32 #include <mui/Rawimage_mcc.h>
34 #include "pageview_class.h"
35 #include "toolbar_class.h"
36 #include "documentlayout_class.h"
37 #include "documentview_class.h"
38 #include "toolbar_images.h"
40 #include "util.h"
41 #include "poppler.h"
42 #include "../locale.h"
45 struct Data
47 Object *btnZoomIn;
48 Object *btnZoomOut;
49 Object *strZoom;
50 Object *btnZoomPopup;
51 Object *btnRotateRight;
52 Object *btnRotateLeft;
53 Object *btnLayouts[4];
54 Object *btnOutline;
55 Object *btnDragScroll;
56 Object *btnDragMark;
58 Object *document;
59 int zoomid;
62 static float presets[] = {
63 8.33f, 12.5f, 25.0f, 33.33f, 50.0f, 66.67f, 75.0f, 100.0f, 125.0f, 150.0f, 200.0f, 300.0f, 400.0f, 600.0f, 800.0f, 1000.0f};
65 enum
67 MEN_ZOOM = 1,
68 MEN_ZOOM_FITWIDTH, MEN_ZOOM_FITPAGE,
69 MEN_ZOOM_PRESET0,
72 struct layoutmode
74 char name[128];
75 int mode;
78 static struct layoutmode layoutmodes[4];
82 * MUI Standard methods
85 /// dispose
87 DEFDISP
89 GETDATA;
91 /* dispose parent class */
93 return(DOSUPER);
96 ////
98 /// new
100 static Object *layoutbutton(char *label, UBYTE *image)
102 return RawimageObject,
103 MUIA_Rawimage_Data, image,
104 MUIA_ShortHelp, label,
105 MUIA_Frame, MUIV_Frame_Button,
106 MUIA_Background, MUII_ButtonBack,
107 MUIA_InputMode, MUIV_InputMode_Immediate,
108 End;
111 DEFNEW
113 Object *btnZoomIn, *btnZoomOut, *btnLayouts[4], *strZoom, *btnRotateLeft, *btnRotateRight, *btnOutline;
114 Object *btnZoomPopup, *btnDragMark, *btnDragScroll;
116 layoutmodes[0].mode = MUIV_DocumentView_Layout_Single;
117 layoutmodes[1].mode = MUIV_DocumentView_Layout_ContinuousSingle;
118 layoutmodes[2].mode = MUIV_DocumentView_Layout_Facing;
119 layoutmodes[3].mode = MUIV_DocumentView_Layout_ContinuousFacing;
120 strncpy(layoutmodes[0].name, LOCSTR( MSG_TOOLBAR_SINGLE ),128);
121 strncpy(layoutmodes[1].name, LOCSTR( MSG_TOOLBAR_CONTINUOUS ),128);
122 strncpy(layoutmodes[2].name, LOCSTR( MSG_TOOLBAR_FACING ),128);
123 strncpy(layoutmodes[3].name, LOCSTR( MSG_TOOLBAR_CONT_FACING ),128);
125 obj = DoSuperNew(cl, obj,
126 MUIA_Group_Horiz, TRUE,
127 Child, strZoom = TextObject,
128 MUIA_Text_PreParse, "\033c",
129 MUIA_Weight, 50,
130 //MUIA_Frame, MUIV_Frame_Text,
131 End,
132 Child, btnZoomPopup = ImageObject,
133 ButtonFrame,
134 MUIA_Image_Spec , MUII_PopUp,
135 MUIA_Image_FontMatchString, "M",
136 MUIA_Image_FontMatchWidth , TRUE,
137 MUIA_Image_FreeVert , TRUE,
138 MUIA_InputMode , MUIV_InputMode_RelVerify,
139 MUIA_Background , MUII_ButtonBack,
140 End,
142 Child, HGroup,
143 MUIA_Weight, 1,
144 Child, btnZoomIn = RawimageObject,
145 MUIA_Rawimage_Data, button_zoomin,
146 MUIA_ShortHelp, LOCSTR( MSG_TOOLBAR_ZOOMIN ),
147 MUIA_Frame, MUIV_Frame_Button,
148 MUIA_InputMode, MUIV_InputMode_RelVerify,
149 MUIA_Background, MUII_ButtonBack,
150 End,
151 Child, btnZoomOut = RawimageObject,
152 MUIA_Rawimage_Data, button_zoomout,
153 MUIA_ShortHelp, LOCSTR( MSG_TOOLBAR_ZOOMOUT ),
154 MUIA_Frame, MUIV_Frame_Button,
155 MUIA_InputMode, MUIV_InputMode_RelVerify,
156 MUIA_Background, MUII_ButtonBack,
157 End,
158 End,
160 Child, HSpace(0),
162 Child, btnRotateLeft = RawimageObject,
163 MUIA_Rawimage_Data, button_rotatecounterclockwise,
164 MUIA_ShortHelp, LOCSTR( MSG_TOOLBAR_CLOCKWISE ),
165 MUIA_Frame, MUIV_Frame_Button,
166 MUIA_InputMode, MUIV_InputMode_RelVerify,
167 MUIA_Background, MUII_ButtonBack,
168 End,
169 Child, btnRotateRight = RawimageObject,
170 MUIA_Rawimage_Data, button_rotateclockwise,
171 MUIA_ShortHelp, LOCSTR( MSG_TOOLBAR_COUNTERCLOCKWISE),
172 MUIA_Frame, MUIV_Frame_Button,
173 MUIA_InputMode, MUIV_InputMode_RelVerify,
174 MUIA_Background, MUII_ButtonBack,
175 End,
176 Child, HSpace(0),
178 Child, btnLayouts[0] = layoutbutton(layoutmodes[0].name, button_singlepage),
179 Child, btnLayouts[1] = layoutbutton(layoutmodes[1].name, button_singlepagecont),
180 Child, btnLayouts[2] = layoutbutton(layoutmodes[2].name, button_doublepage),
181 Child, btnLayouts[3] = layoutbutton(layoutmodes[3].name, button_doublepagecont),
183 Child, HSpace(0),
185 Child, HGroup,
186 MUIA_Weight, 1,
187 Child, btnDragMark = TextObject,
188 MUIA_Frame , MUIV_Frame_Button,
189 MUIA_Text_Contents , LOCSTR( MSG_TOOLBAR_MARK),
190 MUIA_InputMode , MUIV_InputMode_Toggle,
191 MUIA_Background , MUII_ButtonBack,
192 End,
193 Child, btnDragScroll = TextObject,
194 MUIA_Frame , MUIV_Frame_Button,
195 MUIA_Text_Contents , LOCSTR( MSG_TOOLBAR_SCROLL),
196 MUIA_InputMode , MUIV_InputMode_Toggle,
197 MUIA_Background , MUII_ButtonBack,
198 MUIA_Selected , TRUE,
199 End,
200 End,
202 Child, HSpace(0),
204 Child, btnOutline = TextObject,
205 MUIA_Frame , MUIV_Frame_Button,
206 MUIA_Text_Contents , LOCSTR( MSG_TOOLBAR_OUTLINE),
207 MUIA_InputMode , MUIV_InputMode_Toggle,
208 MUIA_Background , MUII_ButtonBack,
209 End,
210 TAG_MORE, INITTAGS);
212 if (obj != NULL)
214 GETDATA;
215 int i;
217 memset(data, 0, sizeof(struct Data));
218 data->strZoom = strZoom;
219 data->btnZoomPopup = btnZoomPopup;
220 data->btnDragMark = btnDragMark;
221 data->btnDragScroll = btnDragScroll;
222 memcpy(data->btnLayouts, btnLayouts, sizeof(btnLayouts));
224 FORTAG(INITTAGS)
226 case MUIA_Toolbar_DocumentView:
227 data->document = (Object*)tag->ti_Data;
228 break;
230 case MUIA_DocumentView_Outline:
231 nnset(btnOutline, MUIA_Selected, tag->ti_Data);
232 break;
234 NEXTTAG
236 DoMethod(btnZoomIn, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, MUIM_Toolbar_Zoom, 10);
237 DoMethod(btnZoomOut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, MUIM_Toolbar_Zoom, -10);
238 DoMethod(btnZoomPopup, MUIM_Notify, MUIA_Selected, FALSE, obj, 1, MUIM_Toolbar_ZoomPopup);
240 DoMethod(btnRotateLeft, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_DocumentView_RotateLeft);
241 DoMethod(btnRotateRight, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_DocumentView_RotateRight);
243 DoMethod(btnOutline, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, obj, 2, MUIM_Toolbar_ToggleOutline, MUIV_TriggerValue);
245 DoMethod(btnDragMark, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, obj, 2, MUIM_Toolbar_DragAction, MUIV_DocumentView_DragAction_Mark);
246 DoMethod(btnDragScroll, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, obj, 2, MUIM_Toolbar_DragAction, MUIV_DocumentView_DragAction_Scroll);
248 for(i=0; i<4; i++)
249 DoMethod(btnLayouts[i], MUIM_Notify, MUIA_Selected, TRUE, obj, 2, MUIM_Toolbar_Layout, i);
253 return (ULONG)obj;
257 ////
259 /// setup
261 DEFMMETHOD(Setup)
263 return DOSUPER;
266 ////
268 /// cleanup
270 DEFMMETHOD(Cleanup)
272 GETDATA;
273 return(DOSUPER);
276 ////
278 /// zoom - format string
280 static char *zoomFormat(int zoomid, char *buffer, int buffersize)
282 if (zoomid == MEN_ZOOM_FITPAGE)
283 snprintf(buffer, buffersize, LOCSTR( MSG_TOOLBAR_FITPAGE));
284 else if (zoomid == MEN_ZOOM_FITWIDTH)
285 snprintf(buffer, buffersize, LOCSTR( MSG_TOOLBAR_FITWIDTH));
286 else
288 float zoom = presets[zoomid - MEN_ZOOM_PRESET0] / 100.0f;
289 snprintf(buffer, buffersize, "%.2f%%", zoom * 100.0f);
292 return buffer;
295 ////
297 /// set
299 DEFSET
301 GETDATA;
302 int i;
304 FORTAG(INITTAGS)
306 case MUIA_Toolbar_DocumentView:
307 data->document = (Object*)tag->ti_Data;
309 /* update state depending on assigned document view. for now only layout mode + outlines */
311 for(i=0; i<4; i++)
312 nnset(data->btnLayouts[i], MUIA_Selected, layoutmodes[i].mode == xget(data->document, MUIA_DocumentView_Layout));
314 /* setup zoom mode and factor (externalize if needed in other situations) */
317 char buff[32];
318 int scaling = xget(data->document, MUIA_DocumentLayout_Scaling);
319 float zoom = xget(data->document, MUIA_DocumentLayout_Zoom) / 65536.0f;
320 int presnum;
322 for(presnum = 0; presets[presnum] < zoom * 100.0f && presnum < (sizeof(presets) / sizeof(presets[0])); presnum++) {};
324 if (presnum >= (sizeof(presets) / sizeof(presets[0])))
325 break;
327 if (scaling == MUIV_DocumentLayout_Scaling_FitPage)
328 data->zoomid = MEN_ZOOM_FITPAGE;
329 else if (scaling == MUIV_DocumentLayout_Scaling_FitWidth)
330 data->zoomid = MEN_ZOOM_FITWIDTH;
331 else
332 data->zoomid = MEN_ZOOM_PRESET0 + presnum;
334 zoom = presets[presnum] / 100.0f;
336 zoomFormat(data->zoomid, buff, sizeof(buff));
337 set(data->strZoom, MUIA_Text_Contents, buff);
341 break;
343 NEXTTAG;
345 return(DOSUPER);
348 ////
350 /// get
352 DEFGET
354 GETDATA;
356 switch (msg->opg_AttrID)
358 case MUIA_PageView_NeedRefresh:
359 return TRUE;
362 return(DOSUPER);
365 ////
367 /// layout
370 DEFMMETHOD(Toolbar_Layout)
372 GETDATA;
373 int i;
375 for(i=0; i<4; i++)
377 if (i != msg->layout)
378 nnset(data->btnLayouts[i], MUIA_Selected, FALSE);
381 return DoMethod(data->document, MUIM_DocumentView_Layout, layoutmodes[msg->layout].mode);
384 ////
386 /// zoom
388 DEFMMETHOD(Toolbar_Zoom)
390 GETDATA;
392 char buff[32];
393 float zoom = xget(data->document, MUIA_DocumentLayout_Zoom) / 65536.0f;
394 int presnum;
396 for(presnum = 0; presets[presnum] < zoom * 100.0f && presnum < (sizeof(presets) / sizeof(presets[0])); presnum++) {};
398 if (data->zoomid >= MEN_ZOOM_PRESET0)
400 if (msg->change > 0)
401 presnum++;
402 else if (msg->change < 0 && presnum > 0)
403 presnum--;
405 else // move to nearest preset
407 if (msg->change > 0)
410 else if (msg->change < 0 && presnum > 0)
411 presnum--;
414 if (presnum >= (sizeof(presets) / sizeof(presets[0])))
415 return 0;
417 data->zoomid = MEN_ZOOM_PRESET0 + presnum;
418 zoom = presets[presnum] / 100.0f;
420 zoomFormat(data->zoomid, buff, sizeof(buff));
421 SetAttrs(data->document,
422 MUIA_DocumentLayout_Scaling, MUIV_DocumentLayout_Scaling_Zoom,
423 MUIA_DocumentLayout_Zoom, (int)(zoom * 65536),
424 TAG_DONE);
425 //nnset(data->strZoom, MUIA_String_Contents, buff);
426 set(data->strZoom, MUIA_Text_Contents, buff);
428 return 0;
431 ////
433 /// zoom popup
435 DEFMMETHOD(Toolbar_ZoomPopup)
437 GETDATA;
439 char buff[32];
440 float zoom = xget(data->document, MUIA_DocumentLayout_Zoom) / 65536.0f;
441 int presnum, id;
443 Object *menZoom;
446 int i;
447 char *extra[] = {"Fit page", "Fit width"};
448 int extraid[] = {MEN_ZOOM_FITPAGE, MEN_ZOOM_FITWIDTH};
450 menZoom = MenuObject,
451 MUIA_Menu_Title, "Zoom",
452 End;
454 for(i=0; i<sizeof(presets) / sizeof(presets[0]); i++)
456 Object *menPreset;
457 char pval[16];
458 snprintf(pval, sizeof(pval), "%.2f%%", presets[i]);
460 menPreset = MenuitemObject,
461 MUIA_Menuitem_Title, pval,
462 MUIA_Menuitem_CopyStrings, TRUE,
463 MUIA_UserData, MEN_ZOOM_PRESET0 + i,
464 End;
466 DoMethod(menZoom, MUIM_Family_AddTail, menPreset);
469 for(i=0; i<sizeof(extra) / sizeof(extra[0]); i++)
471 Object *menPreset;
473 menPreset = MenuitemObject,
474 MUIA_Menuitem_Title, extra[i],
475 MUIA_UserData, extraid[i],
476 End;
478 DoMethod(menZoom, MUIM_Family_AddTail, menPreset);
482 menZoom = MenustripObject, Child, menZoom, End;
484 #if defined(__AROS__)
485 kprintf("[Toolbar_ZoomPopup] not implemented\n");
486 // FIXME: AROS doesn't have MUIM_Menustrip_Popup
487 #else
488 id = DoMethod(menZoom, MUIM_Menustrip_Popup, data->btnZoomPopup, 0 , _left(data->btnZoomPopup),_bottom(data->btnZoomPopup)+1);
489 #endif
491 if (id != 0)
493 char buff[32];
495 data->zoomid = id;
496 zoomFormat(data->zoomid, buff, sizeof(buff));
497 set(data->strZoom, MUIA_Text_Contents, buff);
499 if (id == MEN_ZOOM_FITWIDTH)
500 set(data->document, MUIA_DocumentLayout_Scaling, MUIV_DocumentLayout_Scaling_FitWidth);
501 else if (id == MEN_ZOOM_FITPAGE)
502 set(data->document, MUIA_DocumentLayout_Scaling, MUIV_DocumentLayout_Scaling_FitPage);
503 else
505 float zoom = presets[id - MEN_ZOOM_PRESET0] / 100.0f;
507 SetAttrs(data->document,
508 MUIA_DocumentLayout_Scaling, MUIV_DocumentLayout_Scaling_Zoom,
509 MUIA_DocumentLayout_Zoom, (int)(zoom * 65536),
510 TAG_DONE);
516 MUI_DisposeObject(menZoom);
518 return 0;
521 ////
523 DEFMMETHOD(DocumentView_RotateLeft)
525 GETDATA;
526 return DoMethodA(data->document, msg);
529 DEFMMETHOD(DocumentView_RotateRight)
531 GETDATA;
532 return DoMethodA(data->document, msg);
535 DEFMMETHOD(Toolbar_ToggleOutline)
537 GETDATA;
538 set(data->document, MUIA_DocumentView_Outline, msg->visible);
539 return TRUE;
542 DEFMMETHOD(Toolbar_DragAction)
544 GETDATA;
546 if (msg->dragaction == MUIV_DocumentView_DragAction_Mark)
547 nnset(data->btnDragScroll, MUIA_Selected, FALSE);
548 else
549 nnset(data->btnDragMark, MUIA_Selected, FALSE);
551 set(data->document, MUIA_DocumentView_DragAction, msg->dragaction);
552 return TRUE;
555 BEGINMTABLE
556 DECMMETHOD(Setup)
557 DECMMETHOD(Cleanup)
558 DECSET
559 DECGET
560 DECNEW
561 DECDISP
562 DECMMETHOD(Toolbar_Zoom)
563 DECMMETHOD(Toolbar_Layout)
564 DECMMETHOD(Toolbar_ZoomPopup)
565 DECMMETHOD(DocumentView_RotateLeft)
566 DECMMETHOD(DocumentView_RotateRight)
567 DECMMETHOD(Toolbar_ToggleOutline)
568 DECMMETHOD(Toolbar_DragAction)
569 ENDMTABLE
571 DECSUBCLASS_NC(MUIC_Group, ToolbarClass)